package commons.conf.base;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.builder.ToStringBuilder;

import commons.conf.ResourceDomain;
import commons.conf.ResourceDomainField;

public class BaseResourceDomain implements ResourceDomain {

	private String domainName; //XML
	private String moduleName; //XML
	private String tableName;  //XML
	private Map<String, ResourceDomainField> fieldMap = new HashMap<String, ResourceDomainField>(); //XML
	
	public List<ResourceDomainField> getForeignFields() {
		List<ResourceDomainField> list = new ArrayList<ResourceDomainField>();
		for (ResourceDomainField f : fieldMap.values()) {
			if(f.isForeign()) {
				list.add(f);
			}
		}
		return list;
	}
	
	public boolean hasForeign() {
		return getForeignFields().size()>0;
	}
	
	public String getDomainName() {
		return domainName;
	}

	public void setDomainName(String domainName) {
		this.domainName = domainName;
	}

	public String getModuleName() {
		return moduleName;
	}

	public void setModuleName(String moduleName) {
		this.moduleName = moduleName;
	}

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public Map<String, ResourceDomainField> getFieldMap() {
		return fieldMap;
	}

	public void setFieldMap(Map<String, ResourceDomainField> fieldMap) {
		this.fieldMap = fieldMap;
	}

	public String getFullName() {
		return moduleName + "." + domainName;
	}
	
	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}

	public ResourceDomainField findFieldByColumnId(String id) {
		for (ResourceDomainField f : fieldMap.values()) {
			if(f.getId().equalsIgnoreCase(id)) {
				return f ;
			}
		}
		return null;
	}

}
